
#include "YaoReader.h"

YaoFFmpegDecoder::YaoFFmpegDecoder(YaoStream * stream)
{
    avCodecContext = avcodec_alloc_context3(nullptr);
    int ret = avcodec_parameters_to_context(avCodecContext, stream->avCodecParameters);
    if( ret < 0){
        printf("avcodec_parameters_to_context fail:%d\n", ret);
    }
    avCodec = avcodec_find_decoder(avCodecContext->codec_id);
}

YaoFFmpegDecoder::~YaoFFmpegDecoder()
{
    if(avCodecContext != nullptr){
        avcodec_free_context(&avCodecContext);
        avCodecContext = nullptr;
    }
}

int YaoFFmpegDecoder::Open()
{
    return avcodec_open2(avCodecContext, avCodec, nullptr);
}

int YaoFFmpegDecoder::SendPacket(YaoPacket * packet)
{
    if(packet == nullptr){
        return avcodec_send_packet(avCodecContext, nullptr);
    }
    return avcodec_send_packet(avCodecContext, packet->avPacket);
}

int YaoFFmpegDecoder::ReceiveFrame(YaoFrame * frame)
{
    return avcodec_receive_frame(avCodecContext, frame->avFrame);
}